package com.linchong.binarysearch.easy;

/**
 * @author linchong
 * @version 1.0
 * @Date: 2020-11-28 18:48
 * @Description: LastPositionOfTarget$
 * find the last position of a target number in a sorted array,return
 * -1 if target does not exist
 */
public class FirstPositionOfTarget {

	public int lastPosition(int[] nums,int target){
		if(nums == null || nums.length==0){
			return -1;
		}
		int start = 0;
		int end = nums.length-1;

		while(start + 1 < end ){
			int mid = start+(end-start)/2;
			if(nums[mid] == target){
				end = mid;
			}else if(nums[mid]<target){
				start = mid;
			}else if(nums[mid]>target){
				end = mid;
			}
		}

		if(nums[start]==target){
			return start;
		}


		if(nums[end]==target){
			return end;
		}


		return -1;
	}

	public static void main(String[] args) {
		int[] nums = {1,2,2,4,5,5};
		FirstPositionOfTarget instance = new FirstPositionOfTarget();
		System.out.println(instance.lastPosition(nums,5));
	}
}
